package yunn.myzhihu.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;
import yunn.myzhihu.R;
import yunn.myzhihu.Utils.URL;
import yunn.myzhihu.base.BaseActivity;

/**
 * Created by Yang on 2016/10/10.
 */
public class WelcomeActivity extends BaseActivity {


    @Bind(R.id.img_welcome)
    ImageView mImgWelcome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        initView();

        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                readyGo(MainActivity.class);
                finish();
            }
        };
        timer.schedule(timerTask,1000*2);
    }

    private void initView(){
        //设置全屏显示图片
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //加载网络图片数据
        OkGo.get(URL.STARTIMG)//
                .tag(this)//
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.i("result", s);
                        try {
                            JSONObject result = new JSONObject(s);
                            Picasso.with(WelcomeActivity.this)
                                    .load(result.getString("img"))
                                    .fit()
                                    .centerCrop()
                                    .into(mImgWelcome);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
    }
}
